
#include "Cpu.h"
#include <future>
#include <QString>
#include <QByteArray>
#include <QHostAddress>

namespace TCServer
{
    Cpu::Cpu()
    {
        setOn("parseError", [](QSslSocket *, QVariantMap) {});
    }

    Cpu::~Cpu()
    {

    }
    inline std::string toStdString(const QString &qstr)
    {
        auto local = qstr.toLocal8Bit();
        return std::string(local.constData(), local.size());
    }
    void Cpu::setOn(const QString &type, TaskProcessFunctionType func)
    {
        tasks.emplace(type.toStdString(), func);
    }

    void Cpu::onMessage(QSslSocket *s, const QString &type, const QVariantMap &args)
    {
        assert(tasks.count(type.toStdString()) > 0);

        TaskProcessFunctionType func = tasks.at(type.toStdString());
        func(s, args);
    }

    Cpu * Cpu::instance()
    {
        if (_instance == nullptr)
            _instance = new Cpu;
        return _instance;
    }

    Cpu * Cpu::_instance = nullptr;

}